package cn.tkb.pet.service.impl;

import cn.tkb.basic.service.impl.BaseServiceImpl;
import cn.tkb.basic.util.CodeGenerateUtils;
import cn.tkb.basic.util.DistanceUtil;
import cn.tkb.basic.util.PageQuery;
import cn.tkb.basic.util.Point;
import cn.tkb.order.domain.PetAcquisitionOrder;
import cn.tkb.order.mapper.PetAcquisitionOrderMapper;
import cn.tkb.org.domain.Employee;
import cn.tkb.org.domain.Shop;
import cn.tkb.org.mapper.EmployeeMapper;
import cn.tkb.org.mapper.ShopMapper;
import cn.tkb.pet.domain.Pet;
import cn.tkb.pet.domain.PetDetail;
import cn.tkb.pet.domain.SearchMasterMsg;
import cn.tkb.pet.mapper.PetDetailMapper;
import cn.tkb.pet.mapper.PetMapper;
import cn.tkb.pet.mapper.SearchMasterMsgMapper;
import cn.tkb.pet.query.SearchMasterMsgQuery;
import cn.tkb.pet.service.ISearchMasterMsgService;
import cn.tkb.user.domain.LoginInfo;
import cn.tkb.user.domain.User;
import cn.tkb.user.mapper.LoginInfoMapper;
import cn.tkb.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;
    /**
     * 寻主消息发布实现
     * @param searchMasterMsg
     * @param loginInfo
     */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, LoginInfo loginInfo) {
        //获取当前登录人的用户id
        User user =userMapper.loadByLoginInfoId(loginInfo.getId());
        //2.获取当前区域最近的门店
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop shop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        //3.保存寻主消息表
        searchMasterMsg.setUser_id(user.getId());
        searchMasterMsg.setShop_id(shop.getId());
        searchMasterMsgMapper.save(searchMasterMsg);
        //4.发送短信通知店长上门收购
        System.out.println("亲您有一条新消息请及时处理！！！");
    }

    /**
     * 用户查询个人发布消息实现
     * @param query
     * @param currentLoginInfo
     * @return
     */
    @Override
    public PageQuery<SearchMasterMsg> userSearchMsg(SearchMasterMsgQuery query, LoginInfo currentLoginInfo) {
        //获取当前登录人的用户id
        User user =userMapper.loadByLoginInfoId(currentLoginInfo.getId());
        query.setUserId(user.getId());
        return super.findPage(query);
    }

    /**
     * 待处理消息实现
     * @param query
     * @param currentLoginInfo
     * @return
     */
    @Override
    public PageQuery<SearchMasterMsg> pending(SearchMasterMsgQuery query, LoginInfo currentLoginInfo) {
        //设置状态为待处理
        query.setState(0);
        Employee employee=employeeMapper.loadByLoginInfoId(currentLoginInfo.getId());
        if(employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.findPage(query);
    }

    /**
     * 查询已处理消息实现
     * @param query
     * @param currentLoginInfo
     * @return
     */
    @Override
    public PageQuery<SearchMasterMsg> processed(SearchMasterMsgQuery query, LoginInfo currentLoginInfo) {
        //设置状态为已处理
        query.setState(1);
        Employee employee=employeeMapper.loadByLoginInfoId(currentLoginInfo.getId());
        if(employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.findPage(query);
    }

    /**
     * 处理寻主消息实现
     * @param pet
     * @param currentLoginInfo
     */
    @Override
    public void handle(Pet pet, LoginInfo currentLoginInfo) {
        //修改消息状态
        searchMasterMsgMapper.updateMsgState4ProcessedById(pet.getSearch_master_msg_id());
        //1.获取当前操作人所属店铺
        Employee employee = employeeMapper.loadByLoginInfoId(currentLoginInfo.getId());
        //2.宠物设置店铺
        pet.setShop_id(employee.getShop_id());
        //3.前端传递寻主消息id，如果有，我就给宠物设置判断这个宠物是来自于哪个渠道的是收购的还是自己饲养的
        //4.保存数据
        //4.1保存宠物信息返回自增长id
        petMapper.save(pet);
        //4.2设置宠物ID并保存宠物详情
        PetDetail detail = pet.getDetail();
        detail.setPet_id(pet.getId());
        petDetailMapper.save(detail);
        //5.生成收购订单
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        PetAcquisitionOrder order = pet2order(pet, msg);
        petAcquisitionOrderMapper.save(order);
        System.out.println("宠物收购成功！支付金额是："+order.getPrice());

        //6.生成支付订单 调用支付
    }
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg msg) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(msg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(msg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(msg.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        return order;
    }
}
